package com.zhangyu.shadow.repair.delegate.maintain;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.zhangyu.shadow.repair.config.RepairItemType;
import com.zhangyu.shadow.repair.config.StateCode;
import com.zhangyu.shadow.repair.delegate.repair.entity.Level0Item;
import com.zhangyu.shadow.repair.delegate.repair.entity.Level1Item;
import com.zhangyu.shadow.repair.delegate.search.entity.ClassificationEntity;
import com.zhangyu.shadow.repair.delegate.search.entity.ProcessEntity;
import com.zhangyu.shadow.repair.delegate.search.entity.TroubleEntity;

import java.util.ArrayList;
import java.util.List;

import st.lib.base.ui.recycler.MultipleField;
import st.lib.base.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2018/3/1
 */

public class MaintainDataConverter {
    public List<MultiItemEntity> converter(String stateCode,
                                           List<ClassificationEntity>
                                                   listClassificationBeans) {
        ArrayList<MultiItemEntity> dataList = new ArrayList<>();
        for (ClassificationEntity bean : listClassificationBeans) {
            Level0Item item0 = new Level0Item(bean.getClassificationName(),
                    bean.getClassificationCode(), StateCode.FINISH_PLAN.equals(stateCode));
            ArrayList<MultipleItemEntity> list = new ArrayList<>();
            List<TroubleEntity> troubleBeans =
                    bean.getListTrouble();
            if (troubleBeans != null) {
                for (TroubleEntity troubleBean : troubleBeans) {
                    List<ProcessEntity>
                            listProcessBeans = troubleBean.getListProcess();
                    final MultipleItemEntity itemEntity = MultipleItemEntity.builder()
                            .setItemType(getType(stateCode))
                            .setField(MultipleField.ID, troubleBean.getId())
                            .setField(MultipleField.NAME, troubleBean.getTroubleName())
                            .setField(RepairItemType.ITEM_CODE, troubleBean.getTroubleCode())
                            .setField(RepairItemType.ITEM_LIST_PROCESS, listProcessBeans)
                            .build();
                    list.add(itemEntity);
                }
            }
            item0.addSubItem(new Level1Item(list));
            dataList.add(item0);
        }
        return dataList;
    }

    public static int getType(String stateCode) {
        int code = -1;
        switch (stateCode) {
            case StateCode.UNDER_PLAN:
                code = RepairItemType.ITEM_UNDER_PLAN;
                break;
            case StateCode.FINISH_PLAN:
                code = RepairItemType.ITEM_FINISH_PLAN;
                break;
            case StateCode.READY_WORK:
                code = RepairItemType.ITEM_READY_WORK;
                break;
            case StateCode.UNDER_WORK:
                code = RepairItemType.ITEM_UNDER_WORK;
                break;
            case StateCode.COMPLETED_WORK:
                code = RepairItemType.ITEM_COMPLETED_WORK;
                break;
            case StateCode.FINISH_WORK:
                code = RepairItemType.ITEM_FINISH_WORK;
                break;
        }
        return code;
    }
}
